package PowerWord.UI.SettingPage;

import PowerWord.UI.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class IntervalSettingBar extends Preference {
	private static final int mMaxValue = 15;
	private static final int mMinValue = 2;
	private int mValue = 0;
	private TextView mValueView = null;
	
	public IntervalSettingBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		setWidgetLayoutResource(R.layout.interval_preference);
	}

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        mValueView = (TextView)view.findViewById(R.id.Number);
        ((ImageButton)view.findViewById(R.id.MinusBtn)).setOnClickListener(
        	new OnClickListener() {
				@Override
				public void onClick(View v) {
					IntervalSettingBar.this.mValue = Math.max(IntervalSettingBar.this.mValue-1
															, IntervalSettingBar.mMinValue);
					IntervalSettingBar.this.UpdateView();
				}
        	});
        ((ImageButton)view.findViewById(R.id.AddBtn)).setOnClickListener(
        	new OnClickListener() {
				@Override
				public void onClick(View v) {
					IntervalSettingBar.this.mValue = Math.min(IntervalSettingBar.this.mValue+1
															, IntervalSettingBar.mMaxValue);
					IntervalSettingBar.this.UpdateView();
				}
        	});
        UpdateView();
    }
    
    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return a.getInteger(index, 0);
    }
    
    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        if (restoreValue) {
            mValue = getPersistedInt(mValue);
        } else {
            int value = (Integer) defaultValue;
            mValue = value;
            persistInt(value);
        }
    }
    
    @Override
    protected Parcelable onSaveInstanceState() {
        final Parcelable superState = super.onSaveInstanceState();
        if (isPersistent()) {
            return superState;
        }
        final SavedState myState = new SavedState(superState);
        myState.value = mValue;
        return myState;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (!state.getClass().equals(SavedState.class)) {
            super.onRestoreInstanceState(state);
            return;
        }
        SavedState myState = (SavedState) state;
        super.onRestoreInstanceState(myState.getSuperState());
        mValue = myState.value;
        notifyChanged();
    }
    
    private static class SavedState extends BaseSavedState {
        int value;
        
        public SavedState(Parcel source) {
            super(source);
            value = source.readInt();
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeInt(value);
        }

        public SavedState(Parcelable superState) {
            super(superState);
        }

        public static final Parcelable.Creator<SavedState> CREATOR =
            new Parcelable.Creator<SavedState>() {
	            public SavedState createFromParcel(Parcel in) {
	                return new SavedState(in);
	            }
	
	            public SavedState[] newArray(int size) {
	                return new SavedState[size];
	            }
        	};
    }
    
    private void UpdateView() {
    	mValueView.setText(String.valueOf(mValue));
    	persistInt(mValue);
    	notifyChanged();
    }
}
